package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import domain.Noticias;
import domain.Usuario;

public class JDBCNoticiasDao implements INoticiasDao {
		
		private Connection conn;
	
		public JDBCNoticiasDao(){
			conn = ConnectionManager.getInstance().checkOut();
		}
		
		public void insertNoticia2(String t, String f, String d, String p,int a,int c,int sin,int soc, int mov){
			String sql = "INSERT INTO noticias(Titulo,Entradilla,Descripcion,Provincia,Cultural,Artistica,Sindical,Sociales,Movimiento15M)values(?,?,?,?,?,?,?,?,?)";
					
	        PreparedStatement stmt = null;
	       
	        try {
	        	  	
	            stmt = conn.prepareStatement(sql);            
	            
	            stmt.setString(1, t);
	            stmt.setString(2, f);
	            stmt.setString(3, d);
	            stmt.setString(4, p);
	            stmt.setInt(5, c);
	            stmt.setInt(6, a);
	            stmt.setInt(7, sin);
	            stmt.setInt(8, soc);
	            stmt.setInt(9, mov);
	            stmt.executeUpdate();
	            	                                
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (stmt != null) {
	                    stmt.close();
	                }
	         } catch (SQLException e) {
	            }
	        }

		}
		
		public List<Noticias> listarNoticias() {
			PreparedStatement stmt = null;
	        ResultSet result = null;
	        List<Noticias> listaNoticias = new LinkedList<Noticias>();
	        
	        try {
	        	String sql = "SELECT * FROM noticias";
	            stmt = conn.prepareStatement(sql);
	            stmt.executeQuery();
	            result = stmt.executeQuery();
	            
	            while (result.next()){
	            	Noticias n= new Noticias();
	            	n.setTitulo(result.getString("Titulo"));
	            	n.setDescripcion(result.getString("Descripcion"));
	            	n.setEntradilla(result.getString("Entradilla"));   
	            	
	            	listaNoticias.add(n);
	            }
	        }catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	        	ConnectionManager.getInstance().checkIn(conn);
	            try {
	                    if (result != null) {
	                            result.close();
	                    }
	                    if (stmt != null) {
	                            stmt.close();
	                    }
	            } catch (SQLException e) {
	            }
	        }
	        return listaNoticias;
		}
		
		

		@Override
		public void borrarNoticia(int idNoticias) {
			// TODO Auto-generated method stub
			
		}

		
		public List<Noticias> listarNoticiaEsp(Usuario user) {
			
			PreparedStatement stmt = null;
	        ResultSet result = null;
	        List<Noticias> listaNoticias = new LinkedList<Noticias>();
	        
	        try {
	        	String sql = "SELECT * FROM noticias WHERE (Provincia= ? && Artistica=? && Cultural=? && Sociales=? && Sindical=?)";
	            stmt = conn.prepareStatement(sql);
	            
	            stmt.setString(1, user.getProvincia() );
	            stmt.setInt(2, user.getArtistica());
	            stmt.setInt(3, user.getCultural());
	            stmt.setInt(4,user.getSocial());
	            stmt.setInt(5, user.getSindical());
	            result = stmt.executeQuery();
	            
	            while (result.next()){
	            	Noticias n= new Noticias();
	            	n.setTitulo(result.getString("Titulo"));
	            	n.setDescripcion(result.getString("Descripcion"));
	            	n.setEntradilla(result.getString("Entradilla"));            	
	            	
	            	listaNoticias.add(n);
	            }
	        }catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	        	ConnectionManager.getInstance().checkIn(conn);
	            try {
	                    if (result != null) {
	                            result.close();
	                    }
	                    if (stmt != null) {
	                            stmt.close();
	                    }
	            } catch (SQLException e) {
	            }
	        }
	        return listaNoticias;
		
		}
	
	public ResultSet listarNoticiasSet(){
		PreparedStatement stmt=null;
        ResultSet result = null;     
        try {        
        	stmt = conn.prepareStatement("SELECT * FROM noticias"); 
        	result = stmt.executeQuery();
            
         }catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
            try {
                    if (result != null) {
                            result.close();
                    }
                    if (stmt != null) {
                            stmt.close();
                    }
            } catch (SQLException e) {
            }
        }
        return result;
	}
	
	
	
}		  